package com.gimi.cloud.jimi.manage.controller.userInfo;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.commonService.common.CommonJxlService;
import com.gimi.cloud.jimi.dto.app.moreInfo.MoreInfoDTO;
import com.gimi.cloud.jimi.dto.userInfo.RdJxlDTO;
import com.gimi.cloud.jimi.dto.userInfo.RdMiguanDTO;
import com.gimi.cloud.jimi.dto.userInfo.RdTdDTO;
import com.gimi.cloud.jimi.manage.service.userInfo.JxlService;
import com.gimi.cloud.jimi.manage.service.userInfo.MoreInfoService;
import com.gimi.cloud.jimi.manage.service.userInfo.RdTdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "moreInfo")
public class MoreInfoController {
    @Autowired
    private JxlService jxlService;
    @Autowired
    private CommonJxlService commonJxlService;
    @Autowired
    private RdTdService rdTdService;
    @Autowired
    private MoreInfoService moreInfoService;


    @RequestMapping(value = "/jxlinfo")
    public void jxlInfo(@RequestBody RdJxlDTO jxlDTO, HttpServletResponse response) {
        Response resp = new Response();
        Map<String, Object> data = new HashMap<>();
        RdJxlDTO jxl = jxlService.getUserJxl(jxlDTO);
        if(jxl != null && "0".equals(jxl.getGetdata()) && !StringUtil.isBlank(jxl.getToken())) {
            //去获取聚信力
            jxl = commonJxlService.saveUserJxlRaw(jxl);
            jxlService.saveUserJxlReport(jxl);
        }

        //3获取失败 2获取成功 1获取中 0未获取
        //聚信力获取成功
        if(jxl != null && "2".equals(jxl.getGetdata())) {
            //聚信力为json
            data.put("juxinli",jxl.getReportData());
            data.put("message", "获取成功");
        } else if(jxl != null && "1".equals(jxl.getGetdata())) {
            data.put("message", "获取中，请稍候...");
        } else {
            data.put("message", "获取失败");
        }

        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/tdinfo")
    public void tdInfo(@RequestBody RdTdDTO param, HttpServletResponse response) {
        Response resp = new Response();
        Map<String, Object> data = new HashMap<>();
        RdTdDTO td = rdTdService.getNewestTd(param);

        //3获取失败 2获取成功 1获取中 0未获取
        //聚信力获取成功
        if(td != null && "2".equals(td.getGetdata())) {
            //聚信力为json
            data.put("tongdun",td.getReportData());
            data.put("message", "获取成功");
        } else if(td != null && "1".equals(td.getGetdata())) {
            data.put("message", "获取中，请稍候...");
        } else {
            data.put("message", "获取失败");
        }

        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/miguanInfo")
    public void miguanInfo(@RequestBody RdMiguanDTO param, HttpServletResponse response) {
        Response resp = new Response();
        Map<String, Object> data = new HashMap<>();

        RdMiguanDTO miguan = jxlService.getUserMiguan(param);
        if(!StringUtil.isBlank(miguan.getOssUrl())) {
            data.put("miguan",miguan.getOssUrl());
            data.put("message", "获取成功");
        } else if(!StringUtil.isBlank(miguan.getResponse())) {
            data.put("miguan",miguan.getResponse());
            data.put("message", "获取成功");
        } else {
            data.put("message", "获取失败");
        }

        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/moreinfo")
    public void moreinfo(@RequestBody MoreInfoDTO param, HttpServletResponse response) {
        Response resp = new Response();
        Map<String, Object> data = new HashMap<>();

        MoreInfoDTO info = moreInfoService.getMoreInfoByUserIdAndType(param.getUserId(), param.getDataType());
        if(info != null && "2".equals(info.getGetdata())) {
            //聚信力为json
            data.put("info",info.getReportData());
            data.put("message", "获取成功");
        } else if(info != null && "1".equals(info.getGetdata())) {
            data.put("message", "获取中，请稍候...");
        } else {
            data.put("message", "获取失败");
        }

        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }
}
